import { mapState } from 'vuex'
import { userLogin, userPhoto } from '@/API/userLogin'
// import _ from 'lodash'
// 用户登录
export const UserLogin = {
  data() {
    return {
      code: '',
      show: false,
      canLogin: true,
      checkedVal: false,
      baseUrl: this.$baseUrl,
      // isAndroid: false,
    }
  },
  methods: {
    async getCode() {
      const [err, res] = await uni.login().catch((err) => err)
      if (err || res.errMsg !== 'login:ok') return uni.$showMsg('登录失败！')
      this.code = res.code
    },
    change(e) {
      this.checkedVal = e
    },
    // 调用子组件方法，公司协议
    openRules() {
      this.$refs.rule.openRul()
    },
    // 新api获取用户信息
    async getUserProfile() {
      // 是否可以登录
      if (!this.checkedVal) {
        return uni.$showMsg('请阅读并同意勾选登录协议')
      }
      if (!this.canLogin) return false
      this.canLogin = false
      //授权登录， 准备参数
      wx.getUserProfile({
        desc: '手机号登录',
        success: (res) => {
          this.getToken(res)
        },
        fail: (err) => {
          this.canLogin = true
          return uni.$showMsg('您取消了登录授权！')
        },
      })
    },
    // 用户授权之后，获取用户的基本信息
    // getUserInfo(e) {
    //   // 是否可以登录
    //   if (!this.checkedVal) {
    //     return uni.$showMsg('请阅读并同意勾选登录协议')
    //   }
    //   if (!this.canLogin) return false
    //   this.canLogin = false

    //   setTimeout(() => {
    //     this.canLogin = true
    //   }, 1000)
    // console.log(e);
    // console.log('uuid1')
    // if (e.detail.errMsg === 'getUserInfo:fail auth deny') {
    //   return uni.$showMsg('您取消了登录授权！')
    // }
    // console.log('uuid')
    // console.log(e.detail);
    // console.log(e.detail.userInfo);
    // this.updateUserInfo(e.detail.userInfo);
    //   this.getToken(e.detail)
    // },
    async getToken(info) {
      let { brand } = this.brandInfo
      const query = {
        brand,
        code: this.code,
        encryptedData: info.encryptedData,
        iv: info.iv,
        // my_user: info.userInfo,
      }
      const { data: loginResult } = await userLogin(query)
      // console.log('loginResult', loginResult)
      this.canLogin = true
      if (loginResult.code !== 200) {
        return uni.$showMsg('登录失败2！')
      }
      // console.log('loginResult', loginResult)
      // 关窗
      this.show = false
      this.saveLoginInfo(loginResult.data)
      console.log("授权登录", loginResult.data);
    },
    // 手机号登录
    async decryptPhoneNumber(e) {
      if (!this.canLogin) return false
      this.canLogin = false
      let { brand } = this.brandInfo
      console.log(e)
      // 获取code
      this.getCode()
      const query = {
        brand,
        code: e.detail.code,
        codeKey: this.code,
      }
      const {
        data: { data, code },
      } = await userPhoto(query)
      this.canLogin = true
      if (code !== 200) {
        return uni.$showMsg('登录失败！')
      }
      console.log('Phone', data)
      let { uuid, avatarUrl, nickName, phoneNumber } = data
      let { name = '', telphone = '', brand_id = '', company_id = '' } = data?.userInfo || {}
      let url = `https://www.kamlung.net/images/qrlogo/wx-${brand}.png`
      console.log('url', url)
      let avatar = avatarUrl ? `${this.baseUrl}/images/avatar/${avatarUrl}` : url
      let loginInfo = {
        uuid,
        name,
        telphone,
        brand_id,
        nickName,
        company_id,
        phoneNumber,
        avatarUrl: avatar,
        avataraName: avatarUrl,
      }
      // 关窗
      this.show = false
      // 保存到vuex
      this.saveLoginInfo(loginInfo)
      this.$emit('loginSuccss', loginInfo)
    },
    isReadRuls() {
      if (!this.checkedVal) {
        return uni.$showMsg('请阅读并同意勾选登录协议')
      }
    },
  },
  computed: {
    ...mapState(['brandInfo',]),
  }
}
